2 QUnit
.module( 'mediawiki.user', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
5 this.server
.respondImmediately
= true;
6 // Cannot stub by simple assignment because read-only.
7 // Instead, stub in tests by using 'delete', and re-create
8 // in teardown using the original descriptor (including its
9 // accessors and readonly settings etc.)
10 this.crypto
= Object
.getOwnPropertyDescriptor( window
, 'crypto' );
11 this.msCrypto
= Object
.getOwnPropertyDescriptor( window
, 'msCrypto' );
13 teardown: function () {
15 Object
.defineProperty( window
, 'crypto', this.crypto
);
17 if ( this.msCrypto
) {
18 Object
.defineProperty( window
, 'msCrypto', this.msCrypto
);
23 QUnit
.test( 'options', function ( assert
) {
24 assert
.ok( mw
.user
.options
instanceof mw
.Map
, 'options instance of mw.Map' );
27 QUnit
.test( 'getters (anonymous)', function ( assert
) {
28 // Forge an anonymous user
29 mw
.config
.set( 'wgUserName', null );
30 mw
.config
.set( 'wgUserId', null );
32 assert
.strictEqual( mw
.user
.getName(), null, 'getName()' );
33 assert
.strictEqual( mw
.user
.isAnon(), true, 'isAnon()' );
34 assert
.strictEqual( mw
.user
.getId(), 0, 'getId()' );
37 QUnit
.test( 'getters (logged-in)', function ( assert
) {
38 mw
.config
.set( 'wgUserName', 'John' );
39 mw
.config
.set( 'wgUserId', 123 );
41 assert
.strictEqual( mw
.user
.getName(), 'John', 'getName()' );
42 assert
.strictEqual( mw
.user
.isAnon(), false, 'isAnon()' );
43 assert
.strictEqual( mw
.user
.getId(), 123, 'getId()' );
45 assert
.strictEqual( mw
.user
.id(), 'John', 'user.id()' );
48 QUnit
.test( 'getGroups (callback)', function ( assert
) {
49 var done
= assert
.async();
50 mw
.config
.set( 'wgUserGroups', [ '*', 'user' ] );
52 mw
.user
.getGroups( function ( groups
) {
53 assert
.deepEqual( groups
, [ '*', 'user' ], 'Result' );
58 QUnit
.test( 'getGroups (Promise)', function ( assert
) {
59 mw
.config
.set( 'wgUserGroups', [ '*', 'user' ] );
61 return mw
.user
.getGroups().then( function ( groups
) {
62 assert
.deepEqual( groups
, [ '*', 'user' ], 'Result' );
66 QUnit
.test( 'getRights (callback)', function ( assert
) {
67 var done
= assert
.async();
69 this.server
.respond( [ 200, { 'Content-Type': 'application/json' },
70 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
73 mw
.user
.getRights( function ( rights
) {
74 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
79 QUnit
.test( 'getRights (Promise)', function ( assert
) {
80 this.server
.respond( [ 200, { 'Content-Type': 'application/json' },
81 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
84 return mw
.user
.getRights().then( function ( rights
) {
85 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
89 QUnit
.test( 'generateRandomSessionId', function ( assert
) {
92 result
= mw
.user
.generateRandomSessionId();
93 assert
.strictEqual( typeof result
, 'string', 'type' );
94 assert
.strictEqual( result
.trim(), result
, 'no whitespace at beginning or end' );
95 assert
.strictEqual( result
.length
, 20, 'size' );
97 result2
= mw
.user
.generateRandomSessionId();
98 assert
.notEqual( result
, result2
, 'different when called multiple times' );
102 QUnit
.test( 'generateRandomSessionId (fallback)', function ( assert
) {
105 // Pretend crypto API is not there to test the Math.random fallback
106 delete window
.crypto
;
107 delete window
.msCrypto
;
108 // Assert that the above actually worked. If we use the wrong method
109 // of stubbing, JavaScript silently continues and we need to know that
110 // it was the wrong method. As of writing, assigning undefined is
111 // ineffective as the window property for Crypto is read-only.
112 // However, deleting does work. (T203275)
113 assert
.strictEqual( window
.crypto
|| window
.msCrypto
, undefined, 'fallback is active' );
115 result
= mw
.user
.generateRandomSessionId();
116 assert
.strictEqual( typeof result
, 'string', 'type' );
117 assert
.strictEqual( result
.trim(), result
, 'no whitespace at beginning or end' );
118 assert
.strictEqual( result
.length
, 20, 'size' );
120 result2
= mw
.user
.generateRandomSessionId();
121 assert
.notEqual( result
, result2
, 'different when called multiple times' );
124 QUnit
.test( 'getPageviewToken', function ( assert
) {
125 var result
= mw
.user
.getPageviewToken(),
126 result2
= mw
.user
.getPageviewToken();
127 assert
.strictEqual( typeof result
, 'string', 'type' );
128 assert
.strictEqual( /^[a-f0-9]{20}$/.test( result
), true, '20 HEX symbols string' );
129 assert
.strictEqual( result2
, result
, 'sticky' );
132 QUnit
.test( 'sessionId', function ( assert
) {
133 var result
= mw
.user
.sessionId(),
134 result2
= mw
.user
.sessionId();
135 assert
.strictEqual( typeof result
, 'string', 'type' );
136 assert
.strictEqual( result
.trim(), result
, 'no leading or trailing whitespace' );
137 assert
.strictEqual( result2
, result
, 'retained' );